﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;
using System.Drawing.Imaging;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using CERT.PageLibrary.Base;

namespace CERT.AcceptanceTests.Features
{
    [Binding]
    public partial class DeleteTeamFeature : TestFixtureBase
    {
        private int TeamsCounts { get; set; }

        [Given("Given I am logged in with username testAdmin and password password")]
        public  void FeatureBackground(Table table1)
        {
            var info = ScenarioContext.Current.ScenarioInfo;
            // Assert.AreEqual("Logging in with valid credentials", info.Title);
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/");
            
                var textField = CurrentDriver.FindElementByName("UserName");
                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", "UserName");
                textField.SendKeys("testAdmin");
            
                 textField = CurrentDriver.FindElementByName("Password");
                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", "Password");
                textField.SendKeys("password");

                var loginButton = CurrentDriver.FindElementByName("Login");

            if (loginButton == null)
                Assert.Fail("Expected to find a button with the value of 'Login'.");

            loginButton.Click();
           // loginButton.Click();
            this.WaitUpTo(1000, "could not open main page");
                      
           
            var navBar = CurrentDriver.FindElementById("main-content");
            if (navBar == null)
                Assert.Fail("Expected to find the navbar");

            


        }

        // For additional details on SpecFlow step definitions see http://go.specflow.org/doc-stepdef

        [When("I open the team page")]
        public void WhenIOpenTheTeamPage()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            WaitUpTo(200, "couldn't find delete button");
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/#/Teams");
        }


        [When("I press the delete button")]
        public void WhenIPresstheDeleteButton()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];

            WaitUpTo(200, "couldn't find delete button");
            var Deletes = CurrentDriver.FindElementsById("deletebutton");
            WaitUpTo(250, "couldn't find delete button");
            TeamsCounts = Deletes.Count;

            var Delete = Deletes.Last();
            if (Delete == null)
                Assert.Fail("Expected to find a Delete Team .");
            else
                Delete.Click();


        }

        [Then("The team will be deleted")]
        public void ThenTheTeamWillBeDeleted()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            WaitUpTo(50, "couldn't find delete button");
            var Deletes = CurrentDriver.FindElementsById("deletebutton");
            WaitUpTo(50, "couldn't find delete button");
                Assert.Less(Deletes.Count,this.TeamsCounts); //"Expected to not find a Delete Team .");
        }
    }
}
